package com.tiyende.kash.loop.weight.mask;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class MaskCardView extends View {
    private static final float FRAME_RATIO_WIDTH = 280f;
    private static final float FRAME_RATIO_HEIGHT = 186f;

    private Paint mBackgroundPaint;
    private Paint mCornerPaint;
    private Path mFramePath;
    private Path mCornerPath;
    private RectF mFrameRect;

    private int mFrameWidth;
    private int mFrameHeight;
    private int mCornerLength;
    private int mCornerThickness;
    private int mCornerRadius;
    private int mBackgroundColor;

    public MaskCardView(Context context) {
        this(context, null);
    }

    public MaskCardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MaskCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 背景半透明画笔
        mBackgroundPaint = new Paint();
        mBackgroundColor = Color.parseColor("#80000000"); // 默认半透明黑色
        mBackgroundPaint.setColor(mBackgroundColor);
        mBackgroundPaint.setStyle(Paint.Style.FILL);
        mBackgroundPaint.setAntiAlias(true);

        // 角落画笔
        mCornerPaint = new Paint();
        mCornerPaint.setColor(Color.parseColor("#FF3ED9AB"));
        mCornerPaint.setStyle(Paint.Style.STROKE);
        mCornerThickness = dp2px(4);
        mCornerPaint.setStrokeWidth(mCornerThickness);
        mCornerPaint.setAntiAlias(true);

        mCornerLength = dp2px(50);
        mCornerRadius = dp2px(12); // 默认圆角半径

        mFramePath = new Path();
        mCornerPath = new Path();
        mFrameRect = new RectF();
    }

    /**
     * 设置圆角半径
     * @param radius 圆角半径(像素)
     */
    public void setCornerRadius(int radius) {
        this.mCornerRadius = radius;
        createFramePath();
        createCornerPaths();
        invalidate();
    }

    /**
     * 设置取景框外的背景颜色
     * @param color 颜色值 (如 Color.parseColor("#80000000"))
     */
    public void setMaskColor(int color) {
        this.mBackgroundColor = color;
        mBackgroundPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        calculateFrameSize(w, h);
        createFramePath();
        createCornerPaths();
    }

    private void calculateFrameSize(int w, int h) {
        // 计算取景框大小，保持280:186比例
        float ratio = FRAME_RATIO_WIDTH / FRAME_RATIO_HEIGHT;
        if (w / ratio <= h) {
            mFrameWidth = (int) (w * 0.8f); // 宽度占80%
            mFrameHeight = (int) (mFrameWidth / ratio);
        } else {
            mFrameHeight = (int) (h * 0.8f); // 高度占80%
            mFrameWidth = (int) (mFrameHeight * ratio);
        }

        // 居中取景框
        int left = (w - mFrameWidth) / 2;
        int top = (h - mFrameHeight) / 3;
        int right = left + mFrameWidth;
        int bottom = top + mFrameHeight;

        mFrameRect.set(left, top, right, bottom);
    }

    private void createFramePath() {
        mFramePath.reset();
        // 创建带圆角的取景框路径
        mFramePath.addRoundRect(mFrameRect, mCornerRadius, mCornerRadius, Path.Direction.CW);
    }

    private void createCornerPaths() {
        mCornerPath.reset();

        float left = mFrameRect.left;
        float top = mFrameRect.top;
        float right = mFrameRect.right;
        float bottom = mFrameRect.bottom;

        // 左上角
        mCornerPath.moveTo(left, top + mCornerLength);
        mCornerPath.lineTo(left, top + mCornerRadius);
        mCornerPath.quadTo(left, top, left + mCornerRadius, top);
        mCornerPath.lineTo(left + mCornerLength, top);

        // 右上角
        mCornerPath.moveTo(right - mCornerLength, top);
        mCornerPath.lineTo(right - mCornerRadius, top);
        mCornerPath.quadTo(right, top, right, top + mCornerRadius);
        mCornerPath.lineTo(right, top + mCornerLength);

        // 右下角
        mCornerPath.moveTo(right, bottom - mCornerLength);
        mCornerPath.lineTo(right, bottom - mCornerRadius);
        mCornerPath.quadTo(right, bottom, right - mCornerRadius, bottom);
        mCornerPath.lineTo(right - mCornerLength, bottom);

        // 左下角
        mCornerPath.moveTo(left + mCornerLength, bottom);
        mCornerPath.lineTo(left + mCornerRadius, bottom);
        mCornerPath.quadTo(left, bottom, left, bottom - mCornerRadius);
        mCornerPath.lineTo(left, bottom - mCornerLength);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 保存图层
        int saved = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);

        // 绘制半透明背景
        canvas.drawRect(0, 0, getWidth(), getHeight(), mBackgroundPaint);

        // 使用PorterDuff.Mode.CLEAR清除取景框区域
        mBackgroundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPath(mFramePath, mBackgroundPaint);
        mBackgroundPaint.setXfermode(null);

        // 恢复图层
        canvas.restoreToCount(saved);

        // 绘制四个圆角标记
        canvas.drawPath(mCornerPath, mCornerPaint);
    }

    public RectF getFrameRect() {
        return mFrameRect;
    }

    private int dp2px(float dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
